package com.cskaoyan._05servlet_start;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Macro_Ray
 * @since 2024/4/30 下午8:46
 */

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.从 request对象中获取请求中的任何信息
        String username = req.getParameter("username");   // 根据参数名获取参数值

        //System.out.println(username);
        // 2.处理业务的代码
        String info = "YES";
        if ("admin".equals(username)){
            info = "NO";
        }

        // 3.将要响应的数据放入response
        // 应该设置响应头信息，告诉浏览器响应的内容类型
        // resp.setHeader("Content-Type" ,"text/html");
        resp.setContentType("text/html;charset=UTF-8");

        PrintWriter writer = resp.getWriter();   // 该方法返回一个向响应体中打印字符串的 打印流
        writer.write(info);

    }

}
